package tools

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/smartwalle/alipay/v3"

	"math/rand"

	"net/url"
	"time"
)

func Alipay(g *gin.Context) {
	appID := "9021000134670683"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              //你的appID
	privateKey := "MIIEpAIBAAKCAQEA2M85FgSuTCvZpfRq9KfjZGGuPwsnTSrbtJEWqqHw9utYjTaqyggnpfgMSCJIg94eBw5LNX5k/UqNA8E6XJ9w7zdfZGkurA/nniHryEwaubk7n/Y6kpYzxA324xDvrkJOhiqchBBOxBcReNT4aTesyN2ITwASDaoAjagUyJ6DIcdr/+t4tsm8bNjotdFCfCZ5h1Zzv0ZO51mX6S4zNoLdFKU5pTHuWO8KA8ab8x3lYhiNjHkEWW5ofVuigYM5/9F8SSElcSAg4ezFoUBfj6vhWRf3BxjjwbDMkifofrwlFQlimRfNYIbR8z/5RE1ieULUk3oHFOijzC4cGIiagXauNQIDAQABAoIBAGBboOx3QhvadU37ljmMEFUB0NZ2F46FWMYkoaVOefDT4jRIspk+DJkH6DV6/y2UX1YVdTmeJjYolwmvLEgUGLZIrt3KnZKdMvyl6MvJfytB9wz9+0RA+1rMNgju8wdIsuuQLqXHNaqS8hH43TquIt3XD4jE6EEoaDyNEkejo3Y0APfzstk2fl/eRgG4zH6eZUwLDjqcdYMS6EtgcklCPS8S0s01TfGvDolFmQDWgBcMcOFYvxL/JOycQY33dvLYCbdbY/zG5Y6V/Uq5vbAAJNzcc7srVa6IcIhhSw7cEmw/HY6P9vxTg6fRGXkBS5Lk8VTt9dPdGc3Ox49xSR8f0WECgYEA8zLcLMFpuxU3BCrcL/+MOCy0Cwl4ApcTqdZ8WGaA44tYSmn+vDCGqIBMeovVTFyH6C0ZL1QO/q6LdGgNbZ7EwNgC55+Y24iaE8EjwcA9jkWmURTTaTNeeqhXgbQ8k5mMNEr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6vhWRf3BxjjwbDMkifofrwlFQlimRfNYIbR8z/5RE1ieULUk3oHFOijzC4cGIiagXauNQIDAQAB" //支付宝的公钥
	var client, err = alipay.New(appID, privateKey, false)
	if err != nil {
		panic(err)
	}
	err = client.LoadAliPayPublicKey(aliPhublicKey)
	if err != nil {
		panic(err)
	}
	//var p = alipay.TradeWapPay{}
	var p = alipay.TradePagePay{}
	p.NotifyURL = "/Alipay/rollback"                //支付宝回调
	p.ReturnURL = "http://192.168.2.129:8080/index" //支付后调转页面
	p.Subject = "测试-订单支付"                           //标题
	p.OutTradeNo = generateOrderNumber()            //传递一个唯一单号
	p.TotalAmount = "10.00"                         //金额
	//p.ProductCode = "QUICK_WAP_WAY"
	p.ProductCode = "FAST_INSTANT_TRADE_PAY" //网页支付
	var url2 *url.URL
	url2, err = client.TradePagePay(p)
	if err != nil {
		fmt.Println(err)
	}

	var payURL = url2.String()
	//fmt.Println(payURL)
	g.JSON(200, gin.H{
		"payUrl": payURL,
	})
	//triggerCondition := g.PostForm("notify_type") // 获取触发条件

	//-----------------------------------
	//	http.HandleFunc("/alipay/rollback", func (writer http.ResponseWriter, request *http.Request) {
	//		appID := "9021000134670683"//你的appID
	//		privateKey := "MIIEpAIBAAKCAQEA2M85FgSuTCvZpfRq9KfjZGGuPwsnTSrbtJEWqqHw9utYjTaqyggnpfgMSCJIg94eBw5LNX5k/UqNA8E6XJ9w7zdfZGkurA/nniHryEwaubk7n/Y6kpYzxA324xDvrkJOhiqchBBOxBcReNT4aTesyN2ITwASDaoAjagUyJ6DIcdr/+t4tsm8bNjotdFCfCZ5h1Zzv0ZO51mX6S4zNoLdFKU5pTHuWO8KA8ab8x3lYhiNjHkEWW5ofVuigYM5/9F8SSElcSAg4ezFoUBfj6vhWRf3BxjjwbDMkifofrwlFQlimRfNYIbR8z/5RE1ieULUk3oHFOijzC4cGIiagXauNQIDAQABAoIBAGBboOx3QhvadU37ljmMEFUB0NZ2F46FWMYkoaVOefDT4jRIspk+DJkH6DV6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xA1D919Yy3qo9vc9CWtadyEl2RJkNWL5Bu9bvwclzEWBGXnUBWLWhG8g/T5XYut35k/q0zO6OQh5Qm8iftq3Ps1g=="//你的私钥
	//		aliPhublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2M85FgSuTCvZpfRq9KfjZGGuPwsnTSrbtJEWqqHw9utYjTaqyggnpfgMSCJIg94eBw5LNX5k/UqNA8E6XJ9w7zdfZGkurA/nniHryEwaubk7n/Y6kpYzxA324xDvrkJOhiqchBBOxBcReNT4aTesyN2ITwASDaoAjagUyJ6DIcdr/+t4tsm8bNjotdFCfCZ5h1Zzv0ZO51mX6S4zNoLdFKU5pTHuWO8KA8ab8x3lYhiNjHkEWW5ofVuigYM5/9F8SSElcSAg4ezFoUBfj6vhWRf3BxjjwbDMkifofrwlFQlimRfNYIbR8z/5RE1ieULUk3oHFOijzC4cGIiagXauNQIDAQAB"//支付宝的公钥
	//		var client, err = alipay.New(appID, privateKey, false)
	//		request.ParseForm()
	//		fmt.Println("哈哈哈哈***")
	//		// DecodeNotification 内部已调用 VerifySign 方法验证签名
	//		var _, err = client.DecodeNotification(request.Form)
	//		if err != nil {
	//			// 错误处理
	//			fmt.Println(err)
	//			return
	//		}
	//		// 业务处理
	//		// 如果通知消息没有问题，我们需要确认收到通知消息，不然支付宝后续会继续推送相同的消息
	//		alipay.ACKNotification(writer)
	//	})
}

//func HandleAlipayNotification(writer http.ResponseWriter, request *http.Request, client *alipay.Client) {
//	// 解析表单数据
//	err := request.ParseForm()
//	if err != nil {
//		fmt.Println("Failed to parse form: ", err)
//		return
//	}
//
//	// 解码和验证通知消息的签名
//	notification, err := client.DecodeNotification(request.Form)
//	if err != nil {
//		fmt.Println("Failed to decode notification: ", err)
//		return
//	}
//
//	// 业务处理
//	// 根据通知消息进行相应的业务逻辑处理
//	// ...
//
//	// 确认收到通知消息
//	err = alipay.ACKNotification(writer)
//	if err != nil {
//		fmt.Println("Failed to ACK notification: ", err)
//		return
//	}
//}

//func notify(writer http.ResponseWriter, request *http.Request) {
//	request.ParseForm()
//	appID := "9021000134670683"//你的appID
//	privateKey := "MIIEpAIBAAKCAQEA2M85FgSuTCvZpfRq9KfjZGGuPwsnTSrbtJEWqqHw9utYjTaqyggnpfgMSCJIg94eBw5LNX5k/UqNA8E6XJ9w7zdfZGkurA/nniHryEwaubk7n/Y6kpYzxA324xDvrkJOhiqchBBOxBcReNT4aTesyN2ITwASDaoAjagUyJ6DIcdr/+t4tsm8bNjotdFCfCZ5h1Zzv0ZO51mX6S4zNoLdFKU5pTHuWO8KA8ab8x3lYhiNjHkEWW5ofVuigYM5/9F8SSElcSAg4ezFoUBfj6vhWRf3BxjjwbDMkifofrwlFQlimRfNYIbR8z/5RE1ieULUk3oHFOijzC4cGIiagXauNQIDAQABAoIBAGBboOx3QhvadU37ljmMEFUB0NZ2F46FWMYkoaVOefDT4jRIspk+DJkH6DV6/y2UX1YVdTmeJjYolwmvLEgUGLZIrt3KnZKdMvyl6MvJfytB9wz9+0RA+1rMNgju8wdIsuuQLqXHNaqS8hH43TquIt3XD4jE6EEoaDyNEkejo3Y0APfzstk2fl/eRgG4zH6eZUwLDjqcdYMS6EtgcklCPS8S0s01TfGvDolFmQDWgBcMcOFYvxL/JOycQY33dvLYCbdbY/zG5Y6V/Uq5vbAAJNzcc7srVa6IcIhhSw7cEmw/HY6P9vxTg6fRGXkBS5Lk8VTt9dPdGc3Ox49xSR8f0WECgYEA8zLcLMFpuxU3BCrcL/+MOCy0Cwl4ApcTqdZ8WGaA44tYSmn+vDCGqIBMeovVTFyH6C0ZL1QO/q6LdGgNbZ7EwNgC55+Y24iaE8EjwcA9jkWmURTTaTNeeqhXgbQ8k5mMNEr/HWSzei2HviUPKte4sa5G8NNPLCSO3ol01/xGehkCgYEA5DjDppVppEe6K1kJdgUCoqU98D/wIpHIjN814zMt49VTfgTEp3UMdC3BftqlE8YzI7D5wHluSQAVUou+neQPY+8k38zPRur9G5EbNyEpiJD8D/+6GAFF6MDeANGOCFxjcHjizjKzRYg0+Nexa3Dp7TrvQ3yKL6NB7GO/kGs3kH0CgYALq7AnMazqPm1idNAWOZQ4CeUl9nFc5nUaHGHhOCINSV2WTJnd3BJKOcXxx44X2QTonmlr/QOvMx8cGfYzAYB9pKAXD3CdcGO8ePNHU/tgPXVqxK4UiVOKXVO9tLGHf8HbrJGrjm6SeWbfOBYPP0TsuelLm0hcXPDCOyRxTPjw2QKBgQDjLG304D2sPYnfpF16kToyVHH3OG38u0j6kSW+5y8C31Kg2nY2ipi+/NzjPRmdOWmmWYACS92v5NlUta4k1ONNArud2u1pFnikfCivE9+YBVojQtcnFf1U1scl4XKUeAKz7mE2B9nTmOXB3lLJNDhm4IdSkiGrkcMJX7FMPK2usQKBgQDcpknNv8Uy/jVSZoGWzPvKq16QFE/Rs4GJc4rb3FD1M1rWtgK2+KYLyB6s4YDBuHv8tDANLcrU/08JnHx4pRxA1D919Yy3qo9vc9CWtadyEl2RJkNWL5Bu9bvwclzEWBGXnUBWLWhG8g/T5XYut35k/q0zO6OQh5Qm8iftq3Ps1g=="//你的私钥
//
//	var client, err = alipay.New(appID, privateKey, false)
//	var notification, err = client.DecodeNotification(request.Form)
//	if err != nil {
//		log.Println("解析异步通知发生错误", err)
//		return
//	}
//
//	log.Println("解析异步通知成功:", notification.NotifyId)
//
//	// 示例一：使用自定义请求进行查询
//	var p = alipay.NewPayload("alipay.trade.query")
//	p.AddBizField("out_trade_no", notification.OutTradeNo)
//
//	var rsp *alipay.TradeQueryRsp
//	if err = client.Request(p, &rsp); err != nil {
//		log.Printf("异步通知验证订单 %s 信息发生错误: %s \n", notification.OutTradeNo, err.Error())
//		return
//	}
//	if rsp.IsFailure() {
//		log.Printf("异步通知验证订单 %s 信息发生错误: %s-%s \n", notification.OutTradeNo, rsp.Msg, rsp.SubMsg)
//		return
//	}
//
//	log.Printf("订单 %s 支付成功 \n", notification.OutTradeNo)
//
//	client.ACKNotification(writer)
//}

func generateOrderNumber() string {
	timestamp := time.Now().Unix()
	rand.Seed(time.Now().UnixNano())
	randomNum := rand.Intn(9999-1000) + 1000 // 生成一个四位随机数
	orderNumber := fmt.Sprintf("%d%d", timestamp, randomNum)
	return orderNumber
}
